﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Taobao.EF;
using Taobao.Filter;
using Taobao.Models;
using Taobao.Public;

namespace Taobao.Controllers
{
    public class BaseController : Controller
    {
        public int userId = 0;
        public BaseController()
        {

            //var item = Request.Cookies[Constant.userInfo];
            //if (item != null)
            //{

            //    ViewBag.name = HttpUtility.UrlDecode(item.Values["name"], Encoding.GetEncoding("UTF-8"));
            //    ViewBag.userId = item.Values["userId"];
            //}

        }
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            IsLogin();
            bool result = false;
            //controller上是否有特性CheckLogin，以及特性的IsNeedLogin值
            var controllerAttrs = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(CheckLogin), false);

            string control = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            string action = filterContext.ActionDescriptor.ActionName;
            string local = "/" + control + "/Index";

            if (controllerAttrs.Count() > 0)
            {
                var conAttr = controllerAttrs[0] as CheckLogin;
                if (conAttr != null)
                {
                    if (conAttr.IsNeedLogin)
                        result = true;
                    else
                        result = false;
                }

            }
            //action上是否有特性CheckLogin，以及特性的IsNeedLogin值
            var actonAttrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(CheckLogin), false);
            if (actonAttrs.Count() > 0)
            {
                var attr = actonAttrs[0] as CheckLogin;
                if (attr != null)
                {
                    if (attr.IsNeedLogin)
                        result = true;
                    else
                    {
                        result = false;
                    }
                }
            }
            if (!IsLogin() && result)
            {
                CustomClass.InserNoabsoCahce(Constant.returnUrl, local, 5);

                //  filterContext.Result = new RedirectToRouteResult("Login", new RouteValueDictionary(new { controller = "Login", action = "Index" }));
                filterContext.Result = new ViewResult { ViewName = "_LoginPartial" };// new RedirectResult("/Login/Index");
                //如果没有登录，则跳至登陆页k
                // filterContext.RequestContext.HttpContext.Response.Redirect("~/Login/Index");// RedirectToRoute(new { controller = "Login", action = "Imdex" });// Redirect("/Login/Index");
            }
        }
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
        protected bool IsLogin()
        {
            var item = Request.Cookies[Constant.userInfo];
            if (item != null)
            {
                //var curUser=CustomClass.GetCahce(Constant.userInfo);
                //if(curUser!=null)
                //{
                //    var cuItem = (UseInfor)curUser;
                //    ViewBag.count = cuItem.TotalCount;
                //}
                ViewBag.name = HttpUtility.UrlDecode(item.Values["name"], Encoding.GetEncoding("UTF-8"));
                ViewBag.userId = item.Values["userId"];
                userId = Convert.ToInt32(ViewBag.userId);
                return true;
            }

            return false;
        }


    }
}